/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package slcu.service.impl;

import java.util.Date;
import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;

import slcu.model.InscripcionDisciplina;
import slcu.model.InscripcionDisciplinaLog;
import slcu.service.base.InscripcionDisciplinaLocalServiceBaseImpl;
import slcu.service.persistence.InscripcionDisciplinaLogUtil;
import slcu.service.persistence.InscripcionDisciplinaUtil;

/**
 * The implementation of the Inscripcion local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link slcu.service.InscripcionDisciplinaLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author tito
 * @see slcu.service.base.InscripcionDisciplinaLocalServiceBaseImpl
 * @see slcu.service.InscripcionDisciplinaLocalServiceUtil
 */
public class InscripcionDisciplinaLocalServiceImpl
	extends InscripcionDisciplinaLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link slcu.service.InscripcionDisciplinaLocalServiceUtil} to access the Inscripcion local service.
	 */

	public void registrar(String numeroDocumento, Long cue, Integer grado,
			List<Long> listDisciplinaId, long userId) throws SystemException {

		InscripcionDisciplinaUtil.removeByNumeroDocumento(numeroDocumento);

		for (long disciplinaId : listDisciplinaId) {
			InscripcionDisciplina inscripcionDisciplina =
					InscripcionDisciplinaUtil.create(
					CounterLocalServiceUtil.increment());

			inscripcionDisciplina.setNumeroDocumento(numeroDocumento);
			inscripcionDisciplina.setCue(cue);
			inscripcionDisciplina.setGrado(grado.intValue());
			inscripcionDisciplina.setDisciplinaId(disciplinaId);

			InscripcionDisciplinaUtil.update(
					inscripcionDisciplina, true);
		}

		InscripcionDisciplinaLog log = InscripcionDisciplinaLogUtil.
				create(CounterLocalServiceUtil.increment());

		/*
		 * Se loguea la operacion de inscripcion, con el user
		 * y la fecha.
		 */
		log.setNumeroDocumento(numeroDocumento);
		log.setCue(cue);
		log.setGrado(grado.intValue());

		StringBuilder strbld = new StringBuilder("");
		for (long disciplinaId : listDisciplinaId) {
			if (strbld.length() > 0)
				strbld.append(",");

			strbld.append(disciplinaId);
		}

		log.setDisciplinasIds(strbld.toString());
		log.setUserId(userId);
		log.setDate(new Date());

		InscripcionDisciplinaLogUtil.update(
				log, true);

	}

	public List<InscripcionDisciplina> findByNumeroDocumento(
			String numeroDocumento) throws SystemException {
		return InscripcionDisciplinaUtil.findByNumeroDocumento(numeroDocumento);
	}

}
